var express = require('express');
var pool = require('../mysql');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  let name = req.cookies.user;
  pool.query(`select * from user where name = '${name}'`, function (err, result) {
    if(result){
      res.redirect('/index');
    }else{
      res.redirect('/login');
    }
  });
});

//首页
router.get('/index', function(req, res, next) {
  let name = req.cookies.user;
  pool.query(`select * from user where name = '${name}'`, function (err, result) {
    if(result){
      pool.query(`select * from component`, function (err, result) {
        if(result){
          pool.query(`select * from label`, function (err, la_result) {
            if(la_result){
              res.render('index', { title: 'React 组件库', data:result, name:name, label: la_result});
            }else{
              res.render('index', { title: 'React 组件库', data:result, name:name, label: [] });
            }
          });
        }else{
          res.render('index', { title: 'React 组件库', name:name, data:[], label: [] });
        }
      });
    }else{
      res.redirect('/login');
    }
  });
});
module.exports = router;